//************************************************************************
//Developed by Guo Ming Li 2009/2010
//Source code:
//  The source code may be freely modified or used but this credit section may not be removed 
//  The original author must be notified of any modifications plus a copy of 
//  the changed source code
//Contact info: mailto://gming.li@gmail.com
//************************************************************************
using System;
using System.Collections.Generic;
using System.Text;
using log4net;
using log4net.Config;

namespace GML.BizTalk.OperationManager.Library
{
    public class Logger
    {
        protected static readonly ILog log = LogManager.GetLogger(typeof(Logger));
        //private static readonly string AppName = "GML.Services.ServiceBroker";

        //public enum MessageType
        //{
        //    INCOMING_REQUEST_MESSAGE,
        //    INCOMING_REQUEST_RESPONSE_MESSAGE,
        //    OUTGOING_REQUEST_MESSAGE,
        //    OUTGOING_REQUEST_RESPONSE_MESSAGE,
        //    PERFORMANCE,
        //    OTHER,
        //}
        public enum ServerityLevel
        {
            DEBUG, INFO, WARN, ERROR, FATAL
        }


        public static void Configure()
        {
            log4net.Config.XmlConfigurator.Configure();
        }

        public static void doLog(object state)
        {
            //System.Diagnostics.Debug.WriteLine("new thread started [" + Guid.NewGuid().ToString() + "]");
            LogObject logObject = (LogObject)state;
            //Logger logger = new Logger();
            doLog(logObject.GUID, logObject.Object, logObject.SeverityLevel);
        }

        //public static void doLogPerformance(object state)
        //{
        //    PerformanceObject perObject = (PerformanceObject)state;
        //    doLog(perObject.LogObject);
        //}

        //public static void doLogResponsePerformance(object state)
        //{
        //    RequestResponsePerformanceObject obj = (RequestResponsePerformanceObject)state;
        //    ThreadPool.QueueUserWorkItem(Logger.doLog, obj.LogObject);
        //    ThreadPool.QueueUserWorkItem(Logger.doLogPerformance, obj.PerformanceObject);
        //}

        //public static GML.Common.LogMessage.WCFMessage GetWCFMessage(string guid, string clientCode, object obj, Type objType)
        //{
        //    GML.Common.LogMessage.WCFMessage oWCFMessage = new GML.Common.LogMessage.WCFMessage();
        //    GML.Common.LogMessage.Header oHeader = new GML.Common.LogMessage.Header();
        //    oHeader.ApplicationName = System.Reflection.Assembly.GetEntryAssembly().GetName().Name;// AppName;
        //    oHeader.MessageDateTime = DateTime.Now;
        //    oHeader.MessageID = Guid.NewGuid().ToString();
        //    oHeader.ThreadID = (Thread.CurrentThread.ManagedThreadId).ToString();
        //    oHeader.ServerName = Environment.MachineName;
        //    oHeader.ModuleName = System.Reflection.Assembly.GetCallingAssembly().GetName().Name;
        //    oHeader.ClientCode = clientCode;

        //    GML.Common.LogMessage.WCFBody oBody = new GML.Common.LogMessage.WCFBody();
        //    List<GML.Common.LogMessage.Message> oMessages = new List<GML.Common.LogMessage.Message>();
        //    GML.Common.LogMessage.Message oMessage = new GML.Common.LogMessage.Message();

        //    oMessage.Description = (new XMLUtilities()).TrySerialize(obj, objType);
        //    oMessage.LangID = "EN";

        //    oMessages.Add(oMessage);

        //    oBody.MessageSubmissionID = guid;
        //    oBody.Messages = oMessages.ToArray();

        //    oWCFMessage.Header = oHeader;
        //    oWCFMessage.Body = oBody;

        //    return oWCFMessage;
        //}

        public static void doLog(string guid, object obj, ServerityLevel oSevLevel)
        {
            string msg = string.Empty;
            if (obj.GetType().Equals(typeof(string)))
            {
                msg = "[" + guid + "] [" + obj + "]";
            }
            else
            {
                string sSerializedObj = string.Empty;
                try
                {
                    sSerializedObj = GML.Common.Utilities.Xml.SerializeObject(obj, obj.GetType());
                }
                catch (Exception) { }
                msg = "[" + guid + "] [" + sSerializedObj + "]";
            }
            if (oSevLevel.Equals(ServerityLevel.DEBUG))
            {
                log.Debug(msg);
            }
            else if (oSevLevel.Equals(ServerityLevel.ERROR))
            {
                    log.Error(msg);
            }
            else if (oSevLevel.Equals(ServerityLevel.FATAL))
            {
                    log.Fatal(msg);
            }
            else if (oSevLevel.Equals(ServerityLevel.INFO))
            {
                log.Info(msg);
            }
            else
            {
                log.Warn(msg);
            }
        }
    }
    public class LogObject
    {
        private string __guid;
        private object __obj;
        Logger.ServerityLevel __oSevLevel;

        public LogObject(string guid, object obj, Logger.ServerityLevel oSevLevel)
        {
            this.GUID = guid;
            this.Object = obj;
            this.SeverityLevel = oSevLevel;
        }

        public string GUID
        {
            get { return __guid; }
            set { __guid = value; }
        }
        public object Object
        {
            get { return __obj; }
            set { __obj = value; }
        }
        public Logger.ServerityLevel SeverityLevel
        {
            get { return __oSevLevel; }
            set { __oSevLevel = value; }
        }
    }

}
